<?php

/**
 * @author Watanabex
 * @copyright 2011
 * Admin DAO class
 */
//require('classes/utils/DBFunctions.php');
require('to/Admin.php');

class adminsDAO{
	const SEL_ADMINS	= "SELECT * FROM admins WHERE status = 'A';";
	const SEL_ADMIN 	= "SELECT * FROM admins WHERE status = 'A' AND id = %d;";
	const INS_ADMIN 	= "INSERT INTO admins (nombre, apellido_1, apellido_2, email, num_telefono, login, password, fecha_creado, creado_por) VALUES(%s);";
	const UPD_ADMIN 	= "UPDATE admins SET %s WHERE id = %d;";
	const DEL_ADMIN 	= "DELETE FROM admins WHERE id = %d;";
	
	/*This function get all of the Admins
	 * @param none
	 * @returns array with Admin objects
	*/
	public static function getAll(){
		$sSql = self::SEL_ADMINS; //$this->SEL_ADMINS;
		
		$DB = DBFunctions::getInstance();
		$arrAdmins = $DB->execSelect($sSql, array());
		
		return self::populate($arrAdmins);
	}
	
	/*This function gets one admin depending on the adminID
	 * @param int id
	 * @returns array with admin object */
	public static function getAdmin($iID){
		$sSql = self::SEL_ADMIN;
		
		$DB = DBFunctions::getInstance();
		$arrAdmins = $DB->execSelect($sSql, array($iID));
		
		return self::populate($arrAdmins);
	}
	
	/*This function adds one admin to the database
	 * @params string (nombre, apellido_1, apellido_2, email, telefono, login, password)
	 * @returns int number of affected rows
	*/
	public static function addAdmin($sNombre, $sApellido_1, $sApellido_2 = null, $sEmail, $sTelefono = null, $sLogin, $sPassword){
		$sParams = "'$sNombre', '$sApellido_1', '$sApellido_2', '$sEmail', '$sTelefono', '$sLogin', '$sPassword', '".date("Y-m-d H:i:s")."', 0";
		
		$sSql = self::INS_ADMIN;
		
		$DB = DBFunctions::getInstance();
		//$numRows = $DB->displayQ($sSql, array($sParams));
		$numRows = $DB->execQ($sSql, array($sParams));
		
		return $numRows;
	}
	
	/*This function updates one admin of the database
	 * @params string (nombre, apellido_1, apellido_2, email, telefono, login, password) int ID
	 * @returns int number of affected rows
	*/
	public static function updateAdmin($sNombre, $sApellido_1, $sApellido_2 = null, $sEmail, $sTelefono = null, $sLogin, $sPassword, $iID){
		$sParams = "";
		
		$sParams = "nombre = '$sNombre', apellido_1 = '$sApellido_1', apellido_2 = '$sApellido_2', email = '$sEmail', num_telefono = '$sTelefono', login = '$sLogin'";
		
		if($sPassword != 'same'){
			$sParams .= ", password = '$sPassword'";
		}
		
		$sSql = self::UPD_ADMIN;
		
		$DB = DBFunctions::getInstance();
		
		//$numRows = $DB->displayQ($sSql, array($sParams, $iID));
		$numRows = $DB->execQ($sSql, array($sParams, $iID));
		
		return $numRows;
		
	}
	
	/*This function deletes an admin of the database
	 * @params int ID
	 * @returns int number of affected rows
	*/
	public static function deleteAdmin($iID){
		$sSql = self::DEL_ADMIN;
		
		$DB = DBFunctions::getInstance();
		
		//$numRows = $DB->displayQ($sSql, array($iID));
		$numRows = $DB->execQ($sSql, array($iID));
		
		return $numRows;
	}
	
	/*This function get's the result array and builds an admin array object
	 * @param array Admins result
	 * @returns array with Admin objects */
	public static function populate($arrAdmins){
		
		$objAdmins = array();
		
		foreach($arrAdmins as $A){
			$oAdmin = new Admin();
			$oAdmin->setId($A["id"]);
			$oAdmin->setNombre($A["nombre"]);
			$oAdmin->setApellido1($A["apellido_1"]);
			$oAdmin->setApellido2($A["apellido_2"]);
			$oAdmin->setEmail($A["email"]);
			$oAdmin->setNumTelefono($A["num_telefono"]);
			$oAdmin->setFechaCreado($A["fecha_creado"]);
			$oAdmin->setCreadoPor($A["creado_por"]);
			$oAdmin->setStatus($A["status"]);
			$oAdmin->setLogin($A["login"]);
			$oAdmin->setPassword($A["password"]);
			
			array_push($objAdmins, $oAdmin);
		}
		return $objAdmins;
	}
}